package cn.com.bdqn.controller;

import cn.com.bdqn.entity.SalChance;
import cn.com.bdqn.entity.SysRole;
import cn.com.bdqn.service.SysRoleService;
import cn.com.bdqn.utils.ReturnData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sysRole")
@Api(tags = "角色管理接口")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Operation(summary = "角色高级查询")
    @PostMapping("/queryByLike")
    public ReturnData queryByLike(SysRole sysRole, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "15") int pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<SysRole> sysRoles = sysRoleService.queryByLike(sysRole);
            PageInfo pageInfo = new PageInfo(sysRoles,3);
            return ReturnData.ok().put("sysRoles",sysRoles).put("pageInfo",pageInfo);
        }catch (Exception e){
            return ReturnData.currError("角色高级查询异常");
        }
    }

    @Operation(summary = "根据角色编号查询角色")
    @PostMapping("/queryByRoleId")
    public ReturnData queryByRoleId(Long roleId){
        try {
            SysRole sysRole = sysRoleService.queryByRoleId(roleId);
            return ReturnData.ok().put("sysRole",sysRole);
        }catch (Exception e){
            return ReturnData.currError("根据角色编号查询角色异常");
        }
    }

    @Operation(summary = "查询所有角色")
    @PostMapping("/queryAll")
    public ReturnData queryAll(){
        try {
            List<SysRole> sysRoles = sysRoleService.queryAll();
            return ReturnData.ok().put("sysRoles",sysRoles);
        }catch (Exception e){
            return ReturnData.currError("查询所有角色异常");
        }
    }

    @Operation(summary = "修改角色信息")
    @PostMapping("/updateSysRole")
    public ReturnData updateSysRole(SysRole sysRole){
        int result = sysRoleService.updateSysRole(sysRole);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("修改角色信息异常");
        }
    }

    @Operation(summary = "新增角色信息")
    @PostMapping("/addSysRole")
    public ReturnData addSysRole(SysRole sysRole){
        int result = sysRoleService.addSysRole(sysRole);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("修改角色信息异常");
        }
    }

    @Operation(summary = "删除角色信息")
    @PostMapping("/delSysRole")
    public ReturnData delSysRole(Long roleId){
        int result = sysRoleService.delSysRole(roleId);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("删除角色信息异常");
        }
    }
}
